package cn.arikeky.basic.service.impl;

import cn.arikeky.basic.config.BussinessException;
import cn.arikeky.basic.mapper.BaseMapper;
import cn.arikeky.basic.query.BaseQuery;
import cn.arikeky.basic.service.IBaseService;
import cn.arikeky.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        if (null == id){
            throw new BussinessException("id不能为空");
        }
        baseMapper.delete(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        // 查询条数
        Integer totals = baseMapper.queryByCount(query);
        if (null == totals || 0 ==totals){
            return new PageList<>(0,new ArrayList<>());
        }
        // 查询数据
        List<T> departments = baseMapper.queryByPage(query);

        //方到返回里面
        return new PageList<>(totals,departments);
    }

    @Override
    @Transactional
    public void batchDel(List<Long> ids) {
        baseMapper.batchDel(ids);
    }
}
